package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.hotel.saas.core.dao.SalesUserWalletDetailDAO;
import com.ecoolex.hotel.saas.entity.SalesUserWalletDetail;
import com.ecoolex.hotel.saas.entity.SalesUserWalletDetail.Column;
import com.ecoolex.hotel.saas.entity.SalesUserWalletDetailExample;
import com.ecoolex.hotel.saas.entity.SalesUserWalletDetailExample.Criteria;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SalesUserWalletDetailManager {
	
    @Autowired
    private SalesUserWalletDetailDAO dao;
    
    public void create(Integer walletId, Integer type, BigDecimal amount, Integer source, Integer sourceId, String bizDescription, String createUser) {
    	SalesUserWalletDetail detail = new SalesUserWalletDetail();
    	detail.setWalletId(walletId);
    	detail.setType(type);
    	detail.setAmount(amount);
    	detail.setSource(source);
    	detail.setSourceId(sourceId);
    	detail.setBizDescription(bizDescription);
    	detail.setDeleted(SalesUserWalletDetail.NOT_DELETED);
    	detail.setCreateUser(createUser);
    	detail.setCreateTime(Date.from(Instant.now()));
    	detail.setUpdateUser(createUser);
    	detail.setUpdateTime(Date.from(Instant.now()));
    	dao.insert(detail);
    }
    
    public Pagination<SalesUserWalletDetail> queryByWalletId(Integer walletId, Page page){
    	SalesUserWalletDetailExample example = new SalesUserWalletDetailExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andWalletIdEqualTo(walletId).andDeletedEqualTo(SalesUserWalletDetail.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Column.createTime.desc());
    	return Pagination.create(page, 
    		dao.countByExample(example),
    		() -> dao.selectByExample(example));
    }
}